/*******************************************************************************

	UBEC (Uniband Electronic Corp.)
	

	Project : UZ2400 Simple RF API

	File : spif.c
	
	Usage : SPI Interface Function
	
	Platform: U-Zig Power Module with IAR C compiler
	
	Reference: 

		Silicon Laboratories: C8051F124
		UBEC: UZ2400
	
	Note : 	


	Uniband Electronic Corporation
	3F-3, No. 192 Dongguang Rd., Hsinchu, 300 Taiwan
	Tel : 886-3-5729898, Fax : 886-3-5718599
	http: //www.ubec.com.tw


		Copyright (C) 2005 Uniband Electronic Corporation, All rights reserved
					
********************************************************************************/

#include "uz2400_srf.h"

void InitSPI(){// Init MCU's SPI Register   
  P3SEL |= 0x0E;                             // Setup P3 for SPI mode
  U0CTL = CHAR + SYNC + MM + SWRST;         // 8-bit, SPI, Master
  U0TCTL = CKPH + SSEL1 + STC;              // Polarity, SMCLK, 3-wire
  U0BR0 = 0x002;                            // SPICLK = SMCLK/1
  U0BR1 = 0x000;
  U0MCTL = 0x000;
  ME1 = USPIE0;                             // Module enable
  U0CTL &= ~SWRST;                          // SPI enable
}

//void spi_write(byte data){}

void spi_sw(UINT8 Address, UINT8 Value){
  Address = (Address << 1) | 0x0001; //Shfit the Address
  
  DisInt();
  
  //Set up the SFRPAGE
  uz2400_select();
  
  WIF_0;
  TXBUF = Address; //Write the address
  while(!WIF);
  wait_uz2400();
  
  WIF_0;
  TXBUF = Value; //Write the data
  while(!WIF);
  wait_uz2400();
  
  WIF_0;
  wait_uz2400();
  uz2400_deselect();
  
  EnabInt();
}


UINT8 spi_sr(UINT8 Address){
  UINT8 Value;
  
  Address = (Address << 1);
  
  DisInt();
  
  //Set up the SFRPAGE
  uz2400_select();
  
  WIF_0;
  TXBUF = Address; //Write the address
  while(!WIF);
  wait_uz2400();
  
  RIF_0;
  TXBUF = 0; //Write the data (For generating the SCLK)
  while(!RIF);
  wait_uz2400();
  Value = RXBUF;
  
  RIF_0;
  WIF_0;
  wait_uz2400();
  uz2400_deselect();
  
  EnabInt();
  
  return Value;

}

void spi_lw(UINT16 Address, UINT8 Value)
{

  Address = (Address << 5) | 0x8010;

  DisInt();

//Set up the SFRPAGE
  uz2400_select(); //Put the CS to Low

  WIF_0;
  TXBUF = ((UINT8*)&Address)[1]; //Write the address
  while(!WIF);
  wait_uz2400();
  
  WIF_0;
  TXBUF = ((UINT8*)&Address)[0]; //Write the address
  while(!WIF);
  wait_uz2400();
  
  WIF_0;
  TXBUF = Value; //Write the data
  while(!WIF);
  wait_uz2400();

  WIF_0;
  wait_uz2400();
  uz2400_deselect(); //Put the CS to High

  EnabInt();
}

UINT8 spi_lr(UINT16 Address){
	UINT8 Value;
  Address = (Address << 5) | 0x8000;

  DisInt();

//Set up the SFRPAGE
  uz2400_select(); //Put the CS to Low

  WIF_0;
  TXBUF = ((UINT8*)&Address)[1]; //Write the address
  while(!WIF);
  wait_uz2400();
  
  WIF_0;
  TXBUF = ((UINT8*)&Address)[0]; //Write the address
  while(!WIF);
  wait_uz2400();
  
  RIF_0; //clear the flag
  TXBUF = 0; //Write the NULL data
  while(!RIF);
  wait_uz2400();
  Value = RXBUF;
  
  RIF_0;
  WIF_0;//clear the flag
  wait_uz2400();
  uz2400_deselect(); //Put the CS to High

  EnabInt();

  return Value;
}

void spi_fill_fifo(UINT16 Address, UINT8 *DataPtr, UINT8 Length){

  Address = (Address << 5) | 0x8010;
  
  DisInt();
  
  //Set up the SFRPAGE
  uz2400_select(); //Put the CS to Low
  
  WIF_0;
  TXBUF = ((UINT8*)&Address)[1]; //Write the address
  while(!WIF);
  wait_uz2400();
  
  WIF_0; //clear the flag
  TXBUF = ((UINT8*)&Address)[0]; //Write the address
  while(!WIF);
  wait_uz2400();
  
  while (Length--){
    WIF_0;
    TXBUF = *DataPtr++; //Write the data
    while(!WIF);
    wait_uz2400();
  }
  
  WIF_0;
  wait_uz2400();
  uz2400_deselect(); //Put the CS to High
  
  EnabInt();
}


void spi_dump_fifo(UINT16 Address, UINT8 *DataPtr, UINT8 Length)
{

  Address = (Address << 5) | 0x8000;

  DisInt();

//Set up the SFRPAGE
  uz2400_select(); //Put the CS to Low

  WIF_0;
  TXBUF = ((UINT8*)&Address)[1]; //Write the address
  while(!WIF);
  wait_uz2400();
  
  WIF_0;
  TXBUF = ((UINT8*)&Address)[0]; //Write the address
  while(!WIF);
  wait_uz2400();
  
  while(Length--){
    RIF_0;
    TXBUF = 0; //Write the NULL data
    while(!RIF);
    wait_uz2400();
    *DataPtr++ = RXBUF;
  }

  RIF_0;
  WIF_0;
  wait_uz2400();
  uz2400_deselect(); //Put the CS to High

  EnabInt();

  __no_operation();
  __no_operation();
  __no_operation();
  __no_operation();
  __no_operation();
  __no_operation();
  __no_operation();
  __no_operation();
  __no_operation();
  __no_operation();


}


void spi_rd_rx_fifo(UINT8 *DataPtr, UINT8 *RxLength){

  UINT8 Length;
  
  // This is marked because uz2400_read disables the interrupts previously
  // also, this function is only called by uz2400_read
  DisInt();
  
  //Set up the SFRPAGE
  uz2400_select(); //Put the CS to Low
  
  WIF_0;
  TXBUF = 0xE0;      //Write the address
  while(!WIF);
  wait_uz2400();
  
  WIF_0;
  TXBUF = 0x00;      //Write the address
  while(!WIF);
  wait_uz2400();
  
  RIF_0;
  TXBUF = 0; //Write the NULL data
  while(!RIF);
  wait_uz2400();
  Length = RXBUF;
  
  //Length += MAC_EXTEND_DATA;
  Length+=2;//for LQ and RSSI
  *RxLength = Length;

  while(Length--){
    RIF_0;
    TXBUF = 0; //Write the NULL data
    while(!RIF);
    wait_uz2400();
    *DataPtr++ = RXBUF;
  }
  
  RIF_0;
  WIF_0;
  wait_uz2400();
  uz2400_deselect(); //Put the CS to High
  
  EnabInt();

}

